package cn.jvtc.jx.login_sp0604;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import java.io.IOException;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private EditText etUsername;
    private EditText etPassword;
    private CheckBox ckRemember;
    private Button btnLogin;
    SharedPreferences sp;
    SaveDataUtil util;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        etUsername = (EditText) findViewById(R.id.et_username);
        etPassword = (EditText) findViewById(R.id.et_password);
        ckRemember = (CheckBox) findViewById(R.id.ck_remember);
        btnLogin = (Button) findViewById(R.id.btn_login);
        btnLogin.setOnClickListener(this);
        sp=getSharedPreferences("user", Context.MODE_PRIVATE);
        String username=sp.getString("username","");
        etUsername.setText(username);
        util=new SaveDataUtil();
        try {
            String[] data = util.getUserInfo(this);
            String pwd = data[0];
            etPassword.setText(pwd);
        }catch (IOException e){

        }
    }

    @Override
    public void onClick(View v) {
        util=new SaveDataUtil();
        switch (v.getId()) {
            case R.id.btn_login:
                String username=etUsername.getText().toString();
                String password=etPassword.getText().toString();
                if ((ckRemember.isChecked())){
                    sp=getSharedPreferences("user", Context.MODE_PRIVATE);
                    SharedPreferences.Editor editor=sp.edit();
                    editor.putString("username",username);
                    editor.commit();
                    try {
                        util.saveData(MainActivity.this,password);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                break;
        }
    }



}
